import re

# 头和手
head_hand = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '53', '54', '55', '56', '57', '58',
             '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '74', '75', '76', '77', '78',
             '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '95', '96', '97']

# 脚和身体
foot_body = ['14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31',
             '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49',
             '50', '51', '52', '72', '73', '93', '94', '98', '99']

all_keypoints = head_hand + foot_body
find_keypoint_reg = re.compile(r"\d+")


def valid_label(mark_all_keypoints):
    """
    检查标签值是否合法
    :param lis:列表套列表
    :param label_file:
    :param type:
    :return:
    """
    res = []
    for index, keypoint_lis in enumerate(mark_all_keypoints):
        for shape in keypoint_lis:
            # print(keypoint)
            reg_res = find_keypoint_reg.findall(shape.label)
            if reg_res:
                label_name = reg_res[0]
            else:
                label_name = shape.label
            if label_name not in all_keypoints:
                res.append([shape, "非法：" + shape.label + "(" + str(index + 1) + ")"])
    return res


def check_duplication(mark_all_keypoints):
    """
    检查是否重复
    :param mark_all_keypoints:
    :param label_file:
    :return:
    """
    _res = []
    for index, keypoint_lis in enumerate(mark_all_keypoints):
        res = []
        # print(index, keypoint_lis)
        for shape in keypoint_lis:
            reg_res = find_keypoint_reg.findall(shape.label)
            if reg_res:
                label_name = reg_res[0]
            else:
                label_name = shape.label
            if label_name not in res:
                res.append(label_name)
            else:
                _res.append([shape, "重复：" + shape.label + "(" + str(index + 1) + ")"])
    return _res


def check_is_out_point(shapes):
    """
    检查有无溢出点
    """
    _res = []
    # for shape in shapes:
    #     print(shape.points[0].x())
    #     break
    for shape in shapes:
        if shape.shape_type == "rectangle" and len(shape.points) == 2:
            if str(shape.group_id) != shape.label:
                _res.append([shape, "矩形框分组有误" + shape.label + "(" + str(shape.group_id) + ")"])

            for _shape in shapes:
                if _shape.group_id == None:
                    _shape.group_id = 1
                if len(_shape.points) == 1 and shape.group_id == _shape.group_id:
                    points = [(p.x(), p.y()) for p in shape.points]
                    # print(points)
                    x1, y1 = points[0]
                    x2, y2 = points[1]
                    _x1 = min(x1, x2)
                    _y1 = min(y1, y2)
                    _x2 = max(x1, x2)
                    _y2 = max(y1, y2)

                    if _shape.points[0].x() >= _x1 and _shape.points[0].x() <= _x2:
                        pass
                    else:
                        _res.append([_shape, "横坐标溢出点" + _shape.label + "(" + str(_shape.group_id) + ")"])

                    if _shape.points[0].y() >= _y1 and _shape.points[0].y() <= _y2:
                        pass
                    else:
                        _res.append([_shape, "纵坐标溢出点" + _shape.label + "(" + str(_shape.group_id) + ")"])
    return _res


def check_99meta_keypoints(shapes):
    max_group_id = 0
    for shape in shapes:
        if shape.group_id == None:
            shape.group_id = 1
        max_group_id = shape.group_id if max_group_id < shape.group_id else max_group_id

    all_mark_all_keypoints = []
    for _ in range(max_group_id):
        all_mark_all_keypoints.append([])

    for shape in shapes:
        if shape.shape_type == "point":
            if shape.group_id == None:
                shape.group_id = 1
            all_mark_all_keypoints[shape.group_id - 1].append(shape)

    check_all_res = valid_label(all_mark_all_keypoints) + check_duplication(
        all_mark_all_keypoints) + check_is_out_point(shapes)
    return check_all_res
